<?php
class Pagination 
{

	static function stylePage($base_url, $num_items, $per_page, $cur_page, $simple = FALSE) {

		$page_string = '';
		$page = 5;
		$base_url .= strpos($base_url, '?') ? '&amp;' : '?';
		$total_pages = 1;
		if($num_items > $per_page) {
			$offset = 2;
	
			$total_pages = @ceil($num_items / $per_page);
			$pages = $total_pages;
	
			if($page > $pages) {
				$from = 1;
				$to = $pages;
			} else {
				$from = $cur_page - $offset;
				$to = $from + $page - 1;
				if($from < 1) {
					$to = $cur_page + 1 - $from;
					$from = 1;
					if($to - $from < $page) {
						$to = $page;
					}
				} elseif($to > $pages) {
					$from = $pages - $page + 1;
					$to = $pages;
				}
			}
			$seperator = '<span class="page-sep">,</span>';
			$page_string = ($cur_page - $offset > 1 && $pages > $page ? '<a href="'.$base_url.'page=1" class="first">1 ...</a>' : '').
				($cur_page > 1 && !$simple ? '<a href="'.$base_url.'page='.($cur_page - 1).'" class="prev">&lsaquo;&lsaquo;</a>' : '');
			for($i = $from; $i <= $to; $i++) {
				$page_string .= $i == $cur_page ? '<strong>'.$i.'</strong>' :
					'<a href="'.$base_url.'page='.$i.'">'.$i.'</a>'.$seperator;
			}
	
			$page_string .= ($cur_page < $pages && !$simple ? '<a href="'.$base_url.'page='.($cur_page + 1).'" class="next">&rsaquo;&rsaquo;</a>' : '').
				($to < $pages ? '<a href="'.$base_url.'page='.$pages.'" class="last">... '.$total_pages.'</a>' : '').
				(!$simple && $pages > $page  ? '<kbd><input type="text" name="custompage" size="3" onkeydown="if(event.keyCode==13) {window.location=\''.$base_url.'page=\'+this.value; return false;}" /></kbd>' : '');
	
			$page_string = $page_string ? '<div class="pages">'.(!$simple ? '<em>&nbsp;'.$num_items.'&nbsp;</em>' : '').$page_string.'</div>' : '';
		}

		$arr_page_url = array (
			'BASE_URL' => $base_url, 
			'A_' . 'BASE_URL' => addslashes ( $base_url ), 
			'PER_PAGE' => $per_page, 
			'PREVIOUS_PAGE' => $cur_page > 1 && !$simple ?  $base_url.'page='.($cur_page - 1) : '', 
			'NEXT_PAGE' => $cur_page < $pages && !$simple ?  $base_url.'page='.($cur_page + 1) : '', 
			'TOTAL_PAGES' => $total_pages );
		
		$result['page_string'] = $page_string;
		$result['arr_page_url'] = $arr_page_url;		
		return $result;
	}	
	
	static function styleStart($base_url, $num_items, $per_page, $start_item) 
	{
		// Make sure $per_page is a valid value
		$per_page = ($per_page <= 0) ? 1 : $per_page;
		
		$seperator = '<span class="page-sep">,</span>';
		$total_pages = ceil ( $num_items / $per_page );
		
		if ($total_pages == 1 || ! $num_items) {
			return false;
		}
		
		$on_page = floor ( $start_item / $per_page ) + 1;
		$url_delim = (strpos ( $base_url, '?' ) === false) ? '?' : '&amp;';
		
		$page_string = ($on_page == 1) ? '<strong>1</strong>' : '<a href="' . $base_url . '">1</a>';
		
		if ($total_pages > 5) {
			$start_cnt = min ( max ( 1, $on_page - 4 ), $total_pages - 5 );
			$end_cnt = max ( min ( $total_pages, $on_page + 4 ), 6 );
			
			$page_string .= ($start_cnt > 1) ? ' ... ' : $seperator;
			
			for($i = $start_cnt + 1; $i < $end_cnt; $i ++) {
				$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "{$url_delim}start=" . (($i - 1) * $per_page) . '">' . $i . '</a>';
				if ($i < $end_cnt - 1) {
					$page_string .= $seperator;
				}
			}
			
			$page_string .= ($end_cnt < $total_pages) ? ' ... ' : $seperator;
		} else {
			$page_string .= $seperator;
			
			for($i = 2; $i < $total_pages; $i ++) {
				$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "{$url_delim}start=" . (($i - 1) * $per_page) . '">' . $i . '</a>';
				if ($i < $total_pages) {
					$page_string .= $seperator;
				}
			}
		}
		
		$page_string .= ($on_page == $total_pages) ? '<strong>' . $total_pages . '</strong>' : '<a href="' . $base_url . "{$url_delim}start=" . (($total_pages - 1) * $per_page) . '">' . $total_pages . '</a>';
		
		$arr_page_url = array (
			'BASE_URL' => $base_url, 
			'A_' . 'BASE_URL' => addslashes ( $base_url ), 
			'PER_PAGE' => $per_page, 
			'PREVIOUS_PAGE' => ($on_page == 1) ? '' : $base_url . "{$url_delim}start=" . (($on_page - 2) * $per_page), 
			'NEXT_PAGE' => ($on_page == $total_pages) ? '' : $base_url . "{$url_delim}start=" . ($on_page * $per_page), 
			'TOTAL_PAGES' => $total_pages );
		
		$result['page_string'] = $page_string;
		$result['arr_page_url'] = $arr_page_url;
		
		return $result;
	}
}
?>